package pers.jl.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.jl.common.R;
import pers.jl.entity.ShoppingCart;
import pers.jl.service.IShoppingCartService;

import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author luge
 * @since 2023-04-22
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    /**
     * 获取指定用户购物车的数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        List<ShoppingCart> allShopCart = shoppingCartService.getAllShopCart();
        return R.success(allShopCart);
    }


    /**
     * 将菜品或套餐加入购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R addShopCart(@RequestBody ShoppingCart shoppingCart){
        // 加入
        shoppingCartService.addToShopCart(shoppingCart);
        return R.success("加入购物车成功！");
    }

    /**
     * 购物车商品数量减一
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R deleteOne(@RequestBody ShoppingCart shoppingCart){
        shoppingCartService.deleteShopCart(shoppingCart);
        return R.success("购物车单条商品删除成功！");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R clearAll(){
        shoppingCartService.deleteAllShopCart();
        return R.success("清空购物车成功！");
    }
}
